/*
 * CLASS: UserSearch.java
 */

package view;
import model.*;
import javax.swing.DefaultComboBoxModel;
import java.util.ArrayList;

/**
 *
 * @author Samuele Mattiuzzo
 */
public class UserSearch extends javax.swing.JFrame {

    private Library library;
    private User user;
    MainPage main;
    /** Creates new form UserSearch */
    public UserSearch(Library l,User us, MainPage m) {
        library = l;
        user = us;
        main = m;
        initComponents();
        populateAuthors();
        populateGenre();
        populateTypes();
    }

    /**
     * Popola la combo box degli autori
     */
    private void populateAuthors(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboAuthor.getModel();
        tmp.addElement("");
        for(Author a:library.getAuthors())
            tmp.addElement(a.toString());
    }

    /**
     * Popola la combo box dei generi
     */
    private void populateGenre(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboGenre.getModel();
        tmp.addElement("");
        for(Product p:library.getProducts()){
          boolean ins = true;
          for(int i = 0; i < jComboGenre.getItemCount(); i++){
            if(jComboGenre.getItemAt(i).equals(p.getGenre())){
              ins = false;
              break;
            }
          }
          if(ins)
              tmp.addElement(p.getGenre());
        }
    }

    /**
     * Popola la combo box dei tipi
     */
    private void populateTypes(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboType.getModel();
        tmp.addElement("");
        for(Product p:library.getProducts()){
          boolean ins = true;
          for(int i = 0; i < jComboType.getItemCount(); i++){
            if(jComboType.getItemAt(i).equals(p.getType())){
              ins = false;
              break;
            }
          }
          if(ins)
              tmp.addElement(p.getType());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel6 = new javax.swing.JLabel();
        jTextTitle = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCheckAvail = new javax.swing.JCheckBox();
        jComboAuthor = new javax.swing.JComboBox();
        jComboGenre = new javax.swing.JComboBox();
        jButtonSearch = new javax.swing.JButton();
        jComboType = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jButtonClose = new javax.swing.JButton();

        jDialog1.setTitle("ATTENZIONE");
        jDialog1.setBounds(new java.awt.Rectangle(0, 400, 250, 100));

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addContainerGap())
        );

        setTitle("CERCA PRODOTTO");

        jLabel1.setText("TITOLO");

        jLabel2.setText("AUTORE");

        jLabel3.setText("GENERE");

        jLabel4.setText("SOLO DISPONIBILI");

        jCheckAvail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckAvailActionPerformed(evt);
            }
        });

        jButtonSearch.setText("CERCA");
        jButtonSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSearchMouseClicked(evt);
            }
        });

        jLabel5.setText("TIPO");

        jButtonClose.setText("INDIETRO");
        jButtonClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCloseMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel2)
                                    .add(jLabel3)
                                    .add(jLabel1)
                                    .add(jLabel5))
                                .add(44, 44, 44)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jTextTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jComboGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jComboAuthor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jComboType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(layout.createSequentialGroup()
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jCheckAvail))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButtonSearch)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 59, Short.MAX_VALUE)
                        .add(jButtonClose)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jComboAuthor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jComboGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jComboType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(37, 37, 37)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel4)
                    .add(jCheckAvail))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 49, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonClose)
                    .add(jButtonSearch))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckAvailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckAvailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckAvailActionPerformed

    /**
     * Ricerca per autore
     * @param p lista da filtrare
     * @return lista filtrata
     */
    private ArrayList<Product> searchAuthors(ArrayList<Product> p){
        ArrayList<Product> tmp = new ArrayList<Product>();
        for(Product pr:p)
            if(pr.getAuthors().toString().toLowerCase().equals(jComboAuthor.getSelectedItem().toString().toLowerCase()))
                tmp.add(pr);
        return tmp;            
    }

    /**
     * Ricerca per titolo
     * @param p lista da filtrare
     * @return lista filtrata
     */
    private ArrayList<Product> searchTitle(ArrayList<Product> p){
        ArrayList<Product> tmp = new ArrayList<Product>();
        for(Product pr:p)
            if(pr.getTitle().toLowerCase().equals(jTextTitle.getText().toLowerCase()))
                tmp.add(pr);
        return tmp;
    }

    /**
     * Ricerca per genere
     * @param p lista da filtrare
     * @return lista filtrata
     */
    private ArrayList<Product> searchGenre(ArrayList<Product> p){
        ArrayList<Product> tmp = new ArrayList<Product>();
        for(Product pr:p)
            if(pr.getGenre().toLowerCase().equals(jComboGenre.getSelectedItem().toString().toLowerCase()))
                tmp.add(pr);
        return tmp;
    }

    /**
     * Ricerca per tipo
     * @param p lista da filtrare
     * @return lista filtrata
     */
    private ArrayList<Product> searchType(ArrayList<Product> p){
        ArrayList<Product> tmp = new ArrayList<Product>();
        for(Product pr:p)
            if(pr.getType().toString().toLowerCase().equals(jComboType.getSelectedItem().toString().toLowerCase()))
                tmp.add(pr);
        return tmp;
    }

    /**
     * Ricerca per disponibilità
     * @param p lista da filtrare
     * @return lista filtrata
     */
    private ArrayList<Product> searchAvail(ArrayList<Product> p){
        ArrayList<Product> tmp = new ArrayList<Product>();
        for(Product pr:p)
            if(!pr.isRented())
                tmp.add(pr);
        return tmp;
    }
    private void jButtonSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSearchMouseClicked
        // search!
        ArrayList<Product> tmp = library.getProducts();

        if(!jTextTitle.getText().isEmpty())
            tmp = searchTitle(tmp);

        if(jComboAuthor.getSelectedIndex()>0)
            tmp = searchAuthors(tmp);

        if(jComboGenre.getSelectedIndex()>0)
            tmp = searchGenre(tmp);

        if(jComboType.getSelectedIndex()>0)
            tmp = searchType(tmp);

        if(jCheckAvail.isSelected())
            tmp = searchAvail(tmp);

        if(tmp.isEmpty()){
            jLabel6.setText("La ricerca non ha prodotto risultati");
            jDialog1.setVisible(true);
        }else{
            jCheckAvail.setSelected(false);
            jComboAuthor.setSelectedIndex(-1);
            jComboGenre.setSelectedIndex(-1);
            jComboType.setSelectedIndex(-1);
            jTextTitle.setText("");
            this.setVisible(false);
            new UserSearchResult(library,user,tmp,this).setVisible(true);
        }
    }//GEN-LAST:event_jButtonSearchMouseClicked

    private void jButtonCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCloseMouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_jButtonCloseMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckAvail;
    private javax.swing.JComboBox jComboAuthor;
    private javax.swing.JComboBox jComboGenre;
    private javax.swing.JComboBox jComboType;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextTitle;
    // End of variables declaration//GEN-END:variables

}
